package com.hnkywl.sts.action.cwgl;

import com.google.gson.Gson;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.cwgl.FpdrService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.UUIDGenerator;
import com.hnkywl.sts.util.XmlParse;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-30
 * Time: 上午9:57
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/fpdr")
public class FpdrAction extends SimpleActionSupport {
    private FpdrService fpdrService;
    private GnczService gnczService;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //传入的企业机构Id值
    private Integer type;
    //结算单号
    private String jsdh;
    //客户的ID
    private Long glqyId;
    //发票类型ID
    private Long fpzlId;
    //客户的名称
    private String glqyQc;
    //公司的名称
    private String gsJc;
    //申请（查询）开始时间
    private Date sqrqStart;
    //申请（查询）结束时间
    private Date sqrqEnd;
    //销售结算单的ID
    private Long fpId;
    //申报人所在公司的Id
    private Long gsId;
    private Integer fpdrZt;
    private Long jsdId;
    //功能操作
    private List<Gncz> gnczList;
    private Map<String, Object> voucher;
    private Map<String, String> result = new HashMap<String, String>();
    private Map<String, Object> voucherMaster = new HashMap<String, Object>();
    private XmlParse xmlParse;
    private String jsdIdList;
    private List<Map> voucherItem;
    private String gsmc;

    private Date fpdrSystemDate;

    private String bz;

    private Integer attachment;

    private Gson gson = new Gson();

    private String systemDate;

    private Yg yg;

    private YgService ygService;


    @Action("fpdr")
    public String fpdr(){
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //销售结算单的查询分页显示
    @Action("fpdrList")
    public void jsdList() {
        this.renderJson(this.fpdrService.findAllfpdr(type,fpdrZt,jsdh, sqrqStart, sqrqEnd, glqyId, glqyQc, gsId, gsJc, fpzlId, page, rows));
    }

    //发票进入导入页面
    @Action("fpdr_insert_content")
    public String fpdrInsert(){
        gsmc = this.fpdrService.findfpDetail(jsdId).getCggs().getJc().trim();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        systemDate =fmt.format(new Date());
        voucherMaster = fpdrService.findVoucherMain(jsdId);
        voucherMaster.put("createdate", fmt.format(new Date()));
        voucherItem = fpdrService.getVoucherItem(jsdId);
        return SUCCESS;
    }

//    @Action("findfpdrfl")
//    public void findfpdrFl(){
//        renderText(fpdrService.findFpdrfl(jsdId));
//    }

    //发票凭证导入
//    @Action("generate_voucher")
//    public void insertFpdr() {
//        try {
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy");
//            SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM");
//            voucherService.checkJiuqiUsernamePassword();
//            String uuid = UUIDGenerator.getUUID();
//            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
//            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
//            Boolean begin = voucherService.beginGenerateVoucher(jsdId, VoucherType.FP);
//            if (!begin)
//                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
//            voucherMaster.put("createdate",simpleDateFormat.format(fpdrSystemDate));
//            voucherMaster.put("acctyear",simpleDateFormat2.format(fpdrSystemDate));
//            voucherMaster.put("acctperiod",simpleDateFormat3.format(fpdrSystemDate));
//            voucherMaster.put("attachment",attachment);
//            voucherMaster.put("memo",bz);
//            voucher = fpdrService.getVoucher(jsdId,uuid,year,mouth,voucherMaster);
//            Map<String, Object> msgheader = voucherService.getMsgHeader();
//            voucher.put("msgheader", msgheader);
//            String r = voucherService.generateVoucher(voucher);
//            result = xmlParse.getContent(r);
//            //结果回写
//            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
//                String vchrid = result.get(XmlParse.VCHRID);
//                String vchrnum = result.get(XmlParse.VCHRNUM);
//                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, jsdId, VoucherType.FP);
//                if (!success)
//                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            result.put(XmlParse.RESPONSEFLAG, "0");
//            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
//        }
//        renderJson(new Gson().toJson(result));
//    }

    //财务收付导入
    @Action("generate_voucher")
    public void generateVoucher() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM");
        try {
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiUsername())) {
                throw new RuntimeException("您没有财务系统用户名，不具有导入权限");
            }
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiPassword())) {
                throw new RuntimeException("您没有财务系统密码，不具有导入权限");
            }
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
//            Boolean begin = voucherService.beginGenerateVoucher(jsdId, VoucherType.FP);
//            if (!begin)
//                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            //凭证分录
//            voucher = this.cwsfService.getVoucher(cwsfId, uuid, null);
            voucher = this.fpdrService.getU8VoucherItems(jsdId, uuid, null,fpdrSystemDate);
            //凭证头
//            Map<String, Object> msgheader = voucherService.getMsgHeader();
//            voucher.put("msgheader", msgheader);
//            //凭证主表信息
//            Map<String, Object> voucherMaster =
//                    this.fpdrService.getVoucherMaster(jsdId,null, uuid,null,null,yg);
//            //将头部可以修改的部分的修改值重新赋值
//            voucherMaster.put("createdate",simpleDateFormat.format(fpdrSystemDate));
//            voucherMaster.put("acctyear",simpleDateFormat2.format(fpdrSystemDate));
//            voucherMaster.put("acctperiod",simpleDateFormat3.format(fpdrSystemDate));
//            voucherMaster.put("attachment",attachment);
//            voucherMaster.put("memo", VoucherType.FP.getTag()+jsdId);//备注，在凭证删除时使用
//            this.fpdrService.fpChangeDrrq(jsdId,fpdrSystemDate);
//            voucher.put("master", voucherMaster);
//            String r = voucherService.generateVoucher(voucher);
//            result = xmlParse.getContent(r);
//            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
//                String vchrid = result.get(XmlParse.VCHRID);
//                String vchrnum = result.get(XmlParse.VCHRNUM);
//                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, jsdId, VoucherType.FP);
//                if (!success)
//                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
//            }
            voucher.put(XmlParse.RESPONSEFLAG, "0");
            voucher.put(XmlParse.RESPONSEMESSAGE, "后台凭证导入逻辑不全，请联系管理员");
            renderJson(gson.toJson(voucher));//TODO 凭证导入逻辑不全
            return;
        } catch (Exception e) {
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 撤销凭证
     */
    @Action("revoke_voucher")
    public void revokeVoucher() {
        try {
//            voucherService.checkJiuqiUsernamePassword();
//            Boolean begin = voucherService.beginRevokeVoucher(jsdId, VoucherType.FP);
//            if (!begin)
//                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
//            Map<String, Object> msgheader = voucherService.getMsgHeader();
//            Map<String, Object> revokeInfo = this.fpdrService.getRevokeInfo(jsdId);
//            Map<String, Object> info = new HashMap<String, Object>();
//            info.put("msgheader", msgheader);
//            info.put("revokeinfo", revokeInfo);
//            revokeInfo.put("memo", VoucherType.FP.getTag()+jsdId);//备注，在凭证删除时使用
//            String r = voucherService.revokeVoucher(info);
//            result = xmlParse.getContent(r);
//            //结果回写
//            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
//                Boolean success = voucherService.revokeVoucherSuccess(jsdId, VoucherType.FP);
//                if (!success)
//                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
//            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }



    //修改收款审核状态
    @Action("fpdr_combine")
    public void fpdrCombine() {
        try {
            fpdrService.fpdrCombine(jsdIdList);
            renderText("{\"statusCode\":\"200\", \"message\":\"结算单合并成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

//    /**
//     * 撤销凭证
//     */
//    @Action("revoke_voucher")
//    public void revokeVoucher() {
//        try {
//            voucherService.checkJiuqiUsernamePassword();
//            Boolean begin = voucherService.beginRevokeVoucher(jsdId, VoucherType.FP);
//            if (!begin)
//                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
//            Map<String, Object> msgheader = voucherService.getMsgHeader();
//            Map<String, Object> revokeInfo = fpdrService.getRevokeInfo(jsdId);
//            Map<String, Object> info = new HashMap<String, Object>();
//            info.put("msgheader", msgheader);
//            info.put("revokeinfo", revokeInfo);
//            String r = voucherService.revokeVoucher(info);
//            result = xmlParse.getContent(r);
//            //结果回写
//            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
//                Boolean success = voucherService.revokeVoucherSuccess(jsdId, VoucherType.FP);
//                if (!success)
//                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            result.put(XmlParse.RESPONSEFLAG, "0");
//            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
//        }
//        renderJson(new Gson().toJson(result));
//    }

    /**
     * 重置凭证
     */
    @Action("rest_voucher")
    public void restVoucher() {
//        renderJson(voucherService.restVoucher(jsdId, VoucherType.FP));
    }
    //==================get and set ================================
    public List<Map> getVoucherItem() {
        return voucherItem;
    }

    public void setVoucherItem(List<Map> voucherItem) {
        this.voucherItem = voucherItem;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getJsdh() {
        return jsdh;
    }

    public void setJsdh(String jsdh) {
        this.jsdh = jsdh;
    }

    public Long getGlqyId() {
        return glqyId;
    }

    public void setGlqyId(Long glqyId) {
        this.glqyId = glqyId;
    }

    public Long getFpzlId() {
        return fpzlId;
    }

    public void setFpzlId(Long fpzlId) {
        this.fpzlId = fpzlId;
    }

    public String getGlqyQc() {
        return glqyQc;
    }

    public void setGlqyQc(String glqyQc) {
        this.glqyQc = glqyQc;
    }

    public String getGsJc() {
        return gsJc;
    }

    public void setGsJc(String gsJc) {
        this.gsJc = gsJc;
    }

    public Date getSqrqStart() {
        return sqrqStart;
    }

    public void setSqrqStart(Date sqrqStart) {
        this.sqrqStart = sqrqStart;
    }

    public Date getSqrqEnd() {
        return sqrqEnd;
    }

    public void setSqrqEnd(Date sqrqEnd) {
        this.sqrqEnd = sqrqEnd;
    }

    public Long getFpId() {
        return fpId;
    }

    public void setFpId(Long fpId) {
        this.fpId = fpId;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Integer getFpdrZt() {
        return fpdrZt;
    }

    public void setFpdrZt(Integer fpdrZt) {
        this.fpdrZt = fpdrZt;
    }

    public Long getJsdId() {
        return jsdId;
    }

    public void setJsdId(Long jsdId) {
        this.jsdId = jsdId;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public Map<String, Object> getVoucherMaster() {
        return voucherMaster;
    }

    public void setVoucherMaster(Map<String, Object> voucherMaster) {
        this.voucherMaster = voucherMaster;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public String getJsdIdList() {
        return jsdIdList;
    }

    public void setJsdIdList(String jsdIdList) {
        this.jsdIdList = jsdIdList;
    }

    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public Date getFpdrSystemDate() {
        return fpdrSystemDate;
    }

    public void setFpdrSystemDate(Date fpdrSystemDate) {
        this.fpdrSystemDate = fpdrSystemDate;
    }

    public String getBz() {
        return bz;
    }

    public void setBz(String bz) {
        this.bz = bz;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public String getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(String systemDate) {
        this.systemDate = systemDate;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    @Autowired
    public void setFpdrService(FpdrService fpdrService) {
        this.fpdrService = fpdrService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
}
